package org.gl.ui.manger;

import net.tsz.afinal.http.AjaxCallBack;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.os.Build;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.chanceit.ssss.activity.R;

public class AjaxCallBackProxy<T> extends AjaxCallBack<T> implements LoadStatus {

	private View view;
	String activon = "";
	final String TAG = "AjaxCallBackProxy";
	private OnClickListener mOnclickListener;

	public AjaxCallBackProxy(String action, View view) {
		this.view = view;
		this.activon = action;
	}

	public AjaxCallBackProxy(View view) {
		this.view = view;
	}
	public AjaxCallBackProxy(View view,View.OnClickListener mOnclick) {
		this.view = view;
		this.mOnclickListener = mOnclick;
	}

	@Override
	public void onStart() {
		loading();
		super.onStart();

	}

	@Override
	public void onSuccess(T t) {
		loadSuccessed();
		super.onSuccess(t);
	}

	@Override
	public void onFailure(Throwable t, int errorNo, String strMsg) {
		loadFailed(activon + ":" + strMsg);
		super.onFailure(t, errorNo, strMsg);

	}

	@Override
	public void loading() {
		showProgessView(true, "loading...");
	}

	private void showProgessView(final boolean show, String message) {
		// lg.i(TAG , "showProgessView"+show+""+message);
		if (view != null) {
			final View loadView = ((view.getId() == R.id.loading) ? view : view
					.findViewById(R.id.loading));
			View retryButton = view.findViewById(R.id.loading_retry);
			if (retryButton!=null&&retryButton.getVisibility()==View.VISIBLE) {
				view.setVisibility(View.GONE);
			}
			if (loadView != null) {
				TextView tv = (TextView) loadView.findViewById(R.id.loading_tv);
				if ((tv) != null && message != null) {
					tv.setText(message);
				}
				loadView.findViewById(R.id.iv_load).setVisibility(View.GONE);
				loadView.findViewById(R.id.loading_pb).setVisibility(View.VISIBLE);
				if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
					int shortAnimTime = 1500;

					loadView.setVisibility(View.VISIBLE);
					loadView.animate().setDuration(shortAnimTime)
							.alpha(show ? 1 : 0)
							.setListener(new AnimatorListenerAdapter() {
								@Override
								public void onAnimationEnd(Animator animation) {
									loadView.setVisibility(show ? View.VISIBLE
											: View.GONE);
								}
							});

				} else {
					loadView.setVisibility(show ? View.VISIBLE : View.GONE);
				}
			
			}
		}
	}

	private void setProgressBarVis(final boolean show, View loadView2) {
		final View loadView = loadView2.findViewById(R.id.loading_pb);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
			int shortAnimTime = 1500;

			loadView.setVisibility(View.VISIBLE);
			loadView.animate().setDuration(shortAnimTime)
					.alpha(show ? 1 : 0)
					.setListener(new AnimatorListenerAdapter() {
						@Override
						public void onAnimationEnd(Animator animation) {
							loadView.setVisibility(show ? View.VISIBLE
									: View.GONE);
						}
					});

		} else {
			loadView.setVisibility(show ? View.VISIBLE : View.GONE);
		}
	}
	Handler handler = new Handler();
	Runnable dimmssFailVIew = new Runnable() {
		@Override
		public void run() {
			final boolean show =false;
			if (view != null) {
				final View loadView = ((view.getId() == R.id.loading) ? view : view
						.findViewById(R.id.loading));
				if (loadView != null) {
					setProgressBarVis(show, loadView);
					loadView.findViewById(R.id.iv_load).setVisibility(View.VISIBLE);
				}
			}
		}
	};
	@Override
	public void loadFailed(String message) {
		showProgessView(true, message);
		handler.removeCallbacks(dimmssFailVIew);
		handler.postAtTime(dimmssFailVIew, 1000);
		if (mOnclickListener!=null&&view!=null) {
			View retryButton = view.findViewById(R.id.loading_retry);
			if (retryButton!=null) {
				retryButton.setVisibility(View.VISIBLE);
				retryButton.setOnClickListener(mOnclickListener);
			}
		}
	}

	@Override
	public void loadSuccessed() {
		showProgessView(false, null);

	}

}
